﻿using EasyNet.Componets.Core.Inject;
using EasyNet.Componets.Core.Serialization;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Core.Utils
{
    public static class SerializationUtil
    {
        
        #region Json

        private static IJsonSerialization _json;

        public static IJsonSerialization Json
        {
            
            get
            {

                if (_json != null)
                {
                    return _json;
                }
                _json = IoC.Resolve<IJsonSerialization>();

                return _json;

            }

        }

        #endregion

        #region Xml

        private static IXmlSerialization _xml;

        public static IXmlSerialization Xml
        {
            
            get
            {

                if (_xml != null)
                {
                    return _xml;
                }
                try
                {
                    _xml = IoC.Resolve<IXmlSerialization>();
                }
                catch
                {
                    _xml = new EasyNetXmlSerialization();
                }

                return _xml;

            }

        }

        #endregion

        #region HttpQueryString

        private static IHttpQueryStringSerialization _httpQueryString;

        public static IHttpQueryStringSerialization HttpQueryString
        {
            
            get
            {

                if (_httpQueryString != null)
                {
                    return _httpQueryString;
                }
                try
                {
                    _httpQueryString = IoC.Resolve<IHttpQueryStringSerialization>();
                }
                catch
                {
                    _httpQueryString = new EasyNetHttpQueryStringSerialization();
                }

                return _httpQueryString;

            }

        }

        #endregion


        #region Members

        public static void SetInstance(IXmlSerialization xml)
        {
            _xml = xml;
        }

        public static void SetInstance(IJsonSerialization json)
        {
            _json = json;
        }

        public static void SetInstance(IHttpQueryStringSerialization json)
        {
            _httpQueryString = json;
        }
        #endregion
    }
}
